function t(t, a, i) {
    return a in t ? Object.defineProperty(t, a, {
        value: i,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : t[a] = i, t;
}

var a = getApp(), i = require("../../E99BC573A71FB5DF8FFDAD745E5CA4D1.js"), e = require("../../A4730AC3A71FB5DFC21562C4223CA4D1.js"), r = require("../../0BACB9C3A71FB5DF6DCAD1C4BE4CA4D1.js");

Page({
    data: {
        productId: null,
        detail: {},
        winWidth: a.globalData.windowWidth,
        screenHeight: a.globalData.screenHeight,
        defaultData: [],
        skuKeyData: [],
        curAttr: {},
        attrList: [],
        attrKeys: {},
        attrLen: {},
        skuResult: {},
        cantSelectedItems: {},
        clothing: null,
        color: null,
        sizeData: {},
        initSizeData: JSON.stringify({}),
        curSizeData: {},
        curAttrText: {},
        quantity: 1,
        inventory: [],
        cartNum: 0,
        isLogin: a.globalData.isLogin,
        isLogining: !1,
        productAttr: "",
        productPrice: 0,
        colorImgList: [],
        clothImgObj: {},
        detailList: [],
        priceRange: 0,
        clothingSrc: "",
        isCanSubmit: !1,
        curIdx: 0,
        colorList: [],
        colorObj: {},
        attrAnimData: {},
        btnType: "close"
    },
    onLoad: function(t) {
        var i = this, e = t.id;
        this.getProduct(e), this.setData({
            productId: e,
            inventory: a.globalData.productSkuData
        }, function() {
            i.getSkuData();
        });
    },
    onShow: function() {
        var t = a.globalData.isLogin;
        t && this.getCartNum();
        var i = {
            isLogin: t
        }, e = a.globalData.defaultClothingData;
        e ? (i.defaultData = e, this.setDefaultData(e)) : this.getDefaultData(), null !== r._cb && (i.isLogining = !1, 
        r._cb = null);
        var n = a.globalData.pictureDetailList;
        n ? i.detailList = n : this.getDetailList(), this.setData(i);
    },
    onReady: function() {
        this.attrAnim = wx.createAnimation({
            duration: 200,
            timingFunction: "ease-in"
        });
    },
    getProduct: function(t) {
        var a = this;
        wx.showLoading({
            title: "加载中...",
            mask: !0
        }), e.get("/v2/picture/detail", {
            product_id: t
        }).then(function(t) {
            if (wx.hideLoading(), 1001 === t.data.code) {
                var i = t.data.response.data;
                a.setData({
                    detail: i
                }, function() {
                    var t = a.data.inventory;
                    t && t.length && !a.data.priceRange && a.getPriceRange();
                });
            }
        }).catch(function() {
            wx.hideLoading();
        });
    },
    getDefaultData: function() {
        var t = this;
        e.get("/v2/goods").then(function(i) {
            if (1001 === i.data.code) {
                var e = i.data.response.data;
                a.globalData.defaultClothingData = e, t.setData({
                    defaultData: e
                }, function() {
                    t.setDefaultData(e);
                });
            }
        });
    },
    getDetailList: function() {
        var t = this;
        e.get("/v2/goods/goodsdetails").then(function(i) {
            if (1001 === i.data.code) {
                var e = i.data.response.data;
                a.globalData.pictureDetailList = e, t.setData({
                    detailList: e
                });
            }
        });
    },
    setDefaultData: function(t) {
        var a = this, i = [], e = [], r = {}, n = {}, o = {}, s = !0, u = !1, c = void 0;
        try {
            for (var l, d = t[Symbol.iterator](); !(s = (l = d.next()).done); s = !0) {
                var h = l.value, g = {};
                o[h.id] = h.goods_spec[0].positive_picture.thumb;
                for (var f = 0; f < h.goods_spec.length; f++) {
                    var v = h.goods_spec[f];
                    g[v.color_id] = v;
                    var y = {
                        id: v.color_id,
                        value: v.color_value
                    };
                    r[v.color_id] || (r[v.color_id] = y, 0 === v.status && i.push(v)), n[v.color_id] || 0 !== v.status || (n[v.color_id] = y, 
                    e.push(y));
                }
            }
        } catch (t) {
            u = !0, c = t;
        } finally {
            try {
                !s && d.return && d.return();
            } finally {
                if (u) throw c;
            }
        }
        this.setData({
            colorImgList: i,
            colorList: e,
            colorObj: r,
            clothImgObj: o
        }, function() {
            a.getClothingSrc();
        });
    },
    getSkuData: function() {
        var t = this, i = this.data.inventory;
        i && i.length ? (this.getSkuResult(), this.getSizeData(), !this.data.priceRange && this.data.detail.product_id && this.getPriceRange()) : (wx.showLoading({
            title: "加载中...",
            mask: !0
        }), e.get("/v2/productAttribute").then(function(i) {
            if (wx.hideLoading(), 1001 === i.data.code) {
                var e = i.data.response.data;
                a.globalData.productSkuData = e, t.setData({
                    inventory: e
                }, function() {
                    t.getSkuResult(), t.getSizeData(), !t.data.priceRange && t.data.detail.product_id && t.getPriceRange();
                });
            }
        }).catch(function() {
            wx.hideLoading();
        }));
    },
    getCartNum: function() {
        var t = this;
        e.get("/v2/shopping/shoppingNum").then(function(a) {
            1001 === a.data.code && t.setData({
                cartNum: a.data.response.data.number
            });
        });
    },
    getPriceRange: function() {
        var t = this.data.inventory;
        if (t && t.length) {
            t.length;
            var a = t[0].price, i = t[0].price, e = !0, r = !1, n = void 0;
            try {
                for (var o, s = t[Symbol.iterator](); !(e = (o = s.next()).done); e = !0) {
                    var u = o.value.price;
                    u < a && (a = u), u > i && (i = u);
                }
            } catch (t) {
                r = !0, n = t;
            } finally {
                try {
                    !e && s.return && s.return();
                } finally {
                    if (r) throw n;
                }
            }
            var c = this.data.detail;
            a = Math.round((a + c.product_price_pattern_cent) / 100), i = Math.round((i + c.product_price_pattern_cent) / 100), 
            this.setData({
                priceRange: a === i ? a + "" : a + "~" + i
            });
        }
    },
    getSkuResult: function() {
        var t = this, a = this.data.inventory;
        if (a && a.length) {
            var e = i.getSkuResult(a, 1), r = e.skuResult, n = e.skuKeyData, o = {}, s = [], u = {}, c = {}, l = !0, d = !1, h = void 0;
            try {
                for (var g, f = n[Symbol.iterator](); !(l = (g = f.next()).done); l = !0) {
                    var v = g.value;
                    s.push(v.type), c[v.type] = v.attr.length, 1 === v.attr.length || "clothing" === v.type ? o[v.type] = 1 * v.attr[0].id : o[v.type] = null;
                    var y = !0, p = !1, D = void 0;
                    try {
                        for (var b, S = v.attr[Symbol.iterator](); !(y = (b = S.next()).done); y = !0) {
                            var m = b.value;
                            u[v.type + "/" + m.id] = m;
                        }
                    } catch (t) {
                        p = !0, D = t;
                    } finally {
                        try {
                            !y && S.return && S.return();
                        } finally {
                            if (p) throw D;
                        }
                    }
                }
            } catch (t) {
                d = !0, h = t;
            } finally {
                try {
                    !l && f.return && f.return();
                } finally {
                    if (d) throw h;
                }
            }
            r = i.initSKU(r, s, "/"), this.setData({
                skuResult: r,
                skuKeyData: n,
                curAttr: o,
                clothing: o.clothing,
                color: o.color,
                attrList: s,
                attrKeys: u,
                attrLen: c
            }, function() {
                t.getCantSelectedItems(), t.fillShirtAttr();
            });
        }
    },
    getSizeData: function() {
        var a = this.data.inventory;
        if (a && a.length) {
            a.length;
            var i = {}, e = !0, r = !1, n = void 0;
            try {
                for (var o, s = a[Symbol.iterator](); !(e = (o = s.next()).done); e = !0) {
                    var u = o.value;
                    if (!i[u.goods_list.id] || i[u.goods_list.id] && !i[u.goods_list.id][u.size_list.id]) {
                        var c = JSON.parse(JSON.stringify(u.size_list));
                        c.quantity = 0, i[u.goods_list.id] = Object.assign(i[u.goods_list.id] || {}, t({}, u.size_list.id, c));
                    }
                }
            } catch (t) {
                r = !0, n = t;
            } finally {
                try {
                    !e && s.return && s.return();
                } finally {
                    if (r) throw n;
                }
            }
            this.setData({
                sizeData: i,
                initSizeData: JSON.stringify(i)
            });
        }
    },
    fillShirtAttr: function() {
        var t = this, a = this.data, i = a.skuKeyData, e = a.curAttr, r = {}, n = "", o = {}, s = {}, u = !0, c = !1, l = void 0;
        try {
            for (var d, h = i[Symbol.iterator](); !(u = (d = h.next()).done); u = !0) {
                var g = d.value, f = !0, v = !1, y = void 0;
                try {
                    for (var p, D = g.attr[Symbol.iterator](); !(f = (p = D.next()).done); f = !0) {
                        var b = p.value;
                        if (1 * b.id == 1 * e[g.type]) {
                            "size" === g.type && (o = b), r[g.type] = b.name, n += b.name + ",", s[g.type] = b.name;
                            break;
                        }
                    }
                } catch (t) {
                    v = !0, y = t;
                } finally {
                    try {
                        !f && D.return && D.return();
                    } finally {
                        if (v) throw y;
                    }
                }
            }
        } catch (t) {
            c = !0, l = t;
        } finally {
            try {
                !u && h.return && h.return();
            } finally {
                if (c) throw l;
            }
        }
        this.setData({
            productAttr: n.length > 0 ? n.slice(0, -1) : "",
            curSizeData: o,
            curAttrText: s
        }, function() {
            t.getClothingSrc();
        });
    },
    getClothingSrc: function() {
        var t = this.data.defaultData, a = this.data.color, i = this.data.clothing, e = "", r = !0, n = !1, o = void 0;
        try {
            for (var s, u = t[Symbol.iterator](); !(r = (s = u.next()).done); r = !0) {
                var c = s.value;
                if (1 * c.id == 1 * i) if (a) {
                    var l = !0, d = !1, h = void 0;
                    try {
                        for (var g, f = c.goods_spec[Symbol.iterator](); !(l = (g = f.next()).done); l = !0) {
                            var v = g.value;
                            if (1 * v.color_id == 1 * a) {
                                e = v.positive_picture.thumb;
                                break;
                            }
                        }
                    } catch (t) {
                        d = !0, h = t;
                    } finally {
                        try {
                            !l && f.return && f.return();
                        } finally {
                            if (d) throw h;
                        }
                    }
                } else e = c.goods_spec[0].positive_picture.thumb;
                if (e) break;
            }
        } catch (t) {
            n = !0, o = t;
        } finally {
            try {
                !r && u.return && u.return();
            } finally {
                if (n) throw o;
            }
        }
        this.setData({
            clothingSrc: e
        });
    },
    changeShirtAttr: function(t) {
        var a = this, i = t.currentTarget.dataset, e = i.id, r = i.type;
        e *= 1;
        var n = this.data.curAttr, o = this.data.attrLen, s = {};
        if ("clothing" === r) {
            if (s.clothing = e, s.quantity = 1, e !== n[r]) for (var u in o) "clothing" !== u && o[u] > 1 && (n[u] = null);
        } else "color" === r && (s.color = e);
        n[r] = e, s.curAttr = n, this.setData(s, function() {
            a.getCantSelectedItems(), a.fillShirtAttr(), a.getIsCanSubmit();
        });
    },
    getCantSelectedItems: function() {
        var t = this, a = this.data, e = a.curAttr, r = a.attrList, n = a.attrKeys, o = a.skuResult;
        this.setData(i.getCantSelectedItems(e, r, n, o), function() {
            return t.changeProductPrice();
        });
    },
    changeProductPrice: function() {
        var t = this.data, a = t.attrList, i = t.curAttr, e = t.skuResult, r = this.data.detail.product_price_pattern_cent || 0, n = [], o = !0, s = !1, u = void 0;
        try {
            for (var c, l = a[Symbol.iterator](); !(o = (c = l.next()).done); o = !0) {
                var d = c.value;
                for (var h in i) if (i[h] && h === d) {
                    n.push(h + "/" + i[h]);
                    break;
                }
            }
        } catch (t) {
            s = !0, u = t;
        } finally {
            try {
                !o && l.return && l.return();
            } finally {
                if (s) throw u;
            }
        }
        var g = [];
        e[n = n.join(",")] && (g = e[n].prices);
        var f = 0, v = 0, y = g.length;
        y > 0 && (v = g[0] + r, f = g[y - 1] + r), this.setData({
            productPrice: v === f ? Math.round(v / 100) : Math.round(v / 100) + "~" + Math.round(f / 100)
        });
    },
    lookShirtSize: function() {
        wx.navigateTo({
            url: "/pages/shirt/shirtSize"
        });
    },
    bindInputNumblur: function(t) {
        var a = 1 * t.detail.value;
        a || (a = 1), this.setData({
            quantity: a
        });
    },
    handleNum: function(t) {
        var a = t.currentTarget.dataset.type, i = this.data.quantity;
        if ("add" === a) i += 1; else {
            if (1 === i) return;
            i -= 1;
        }
        this.setData({
            quantity: i
        });
    },
    goToCart: function() {
        wx.navigateTo({
            url: "/pages/cart/index"
        });
    },
    getIsCanSubmit: function(t) {
        t = t || this.data.curAttr;
        var a = !0;
        for (var i in t) if (!t[i]) {
            a = !1;
            break;
        }
        return this.setData({
            isCanSubmit: a
        }), a;
    },
    login: function(t) {
        var i = this;
        a.globalData.isLogin || this.data.isLogining ? "function" == typeof t && t() : this.setData({
            isLogining: !0
        }, function() {
            r.login(function(a) {
                i.setData({
                    isLogining: !1
                }), 1001 === a && "function" == typeof t && t();
            });
        });
    },
    handleBtnType: function(t) {
        var a = this;
        this.setData({
            btnType: t.currentTarget.dataset.type
        }, function() {
            a.showAttrBox();
        });
    },
    submitProduct: function() {
        var t = this.data.btnType;
        "close" === t ? this.hideAttrBox() : "cart" === t ? this.addToCart() : this.buyNow();
    },
    addToCart: function() {
        var t = this;
        this.login(function() {
            t.createCartData("cart");
        });
    },
    buyNow: function() {
        var t = this;
        this.login(function() {
            t.createCartData("buy");
        });
    },
    createCartData: function(i) {
        var r = this;
        if (this.getIsCanSubmit()) {
            var n = this.data.curAttr, o = this.data.curAttrText, s = this.data.quantity || 1, u = this.data.curSizeData, c = {
                product_id: this.data.productId,
                goods_id: n.clothing,
                goods_spec_id: this.getGoodsSpecId(n),
                product_size: JSON.stringify(t({}, u.name, s))
            };
            for (var l in o) "size" !== l && (c["product_" + l] = o[l]);
            var d = "/v2/shopping/buyNow";
            "cart" === i && (d = "/v2/shopping/add"), wx.showLoading({
                title: "加载中...",
                mask: !0
            }), e.post(d, c).then(function(t) {
                wx.hideLoading(), 1001 === t.data.code && (r.hideAttrBox(), "buy" === i ? wx.navigateTo({
                    url: "/pages/order/order?cartId=" + t.data.response.data.id
                }) : (wx.showToast({
                    title: "加入购物车成功"
                }), r.getCartNum(), a.globalData.isNeedToUpdateCartData = !0));
            }).catch(function() {
                wx.hideLoading();
            });
        } else wx.showToast({
            title: "请先选择衣服规格",
            icon: "none"
        });
    },
    getGoodsSpecId: function(t) {
        var a = this.data.inventory, i = void 0, e = !0, r = !1, n = void 0;
        try {
            for (var o, s = a[Symbol.iterator](); !(e = (o = s.next()).done); e = !0) {
                var u = o.value;
                1 * u.goods_id == 1 * t.clothing && 1 * u.color_id == 1 * t.color && 1 * u.style_id == 1 * t.type && 1 * u.size_id == 1 * t.size && (i = u.id);
            }
        } catch (t) {
            r = !0, n = t;
        } finally {
            try {
                !e && s.return && s.return();
            } finally {
                if (r) throw n;
            }
        }
        return i;
    },
    hideAttrBox: function() {
        this.handleAttrAnim(!1);
    },
    showAttrBox: function() {
        this.handleAttrAnim(!0);
    },
    handleAttrAnim: function(t) {
        var a = 0;
        t || (a = this.data.screenHeight), this.attrAnim.top(a).step(), this.setData({
            attrAnimData: this.attrAnim.export()
        });
    },
    swiperChange: function(t) {
        this.setData({
            curIdx: t.detail.current
        });
    }
});